function ApplyLog() {
    this.method, this.action;
}

ApplyLog.prototype.load = function (params) {
    var objThis = this;
    var params = params != undefined ? params : $('#inputForm').serializeJson();
    $('#datagrid_ApplyLog').datagrid({
        url: basePath + '/' + this.action + '/' + objThis.method,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        pageSize: 50,
        pageNumber: 1,
        pageList: [20, 50, 100],
        loadMsg: '数据正在加载中...',
        queryParams: params,
        singleSelect: true,
        selectOnCheck: false,
        checkOnSelect: false,
        columns: [[
            {
                field: 'applyLogCreateTime',
                title: '<span class="txtcenter">时间</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'applyLogOperationType',
                title: '<span class="txtcenter">操作类型</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center',
                formatter: function(v, row) {
                	//1房源审核;2人员变更
                	var applyLogOperationType = row.applyLogOperationType;
                	if(applyLogOperationType == 1){
                		return '房源审核';
                	}else if(applyLogOperationType == 2){
                		return '人员变更';
                	}
                	return '';
                }
            },
            {
                field: 'applyLogOperator',
                title: '<span class="txtcenter">操作员</span>',
                resizable: true,
                hidden: false,
                sortable: true,
                width: '200',
                align: 'center'
            },
            {
                field: 'applyLogMobile',
                title: '<span class="txtcenter">手机</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'applyLogMemo',
                title: '<span class="txtcenter">备注/操作内容</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            }
        ]],
        onLoadSuccess: function (data) {
            //validateReqStatus(data);
        },
        onLoadError: function (data) {
            //validateReqStatus(data);
        }
    });
};

ApplyLog.prototype.init = function () {
    var objThis = this;
    objThis.load();
};

//绑定修改房管经纪人事件处理
function bindChangeAgentHandler(){
	$("#changeAgent").click(function(){
		$("#isChangeAll").attr("checked", false);//恢复初始状态
		$("#changeAgent_dialog").show();
		//加载组内经纪人列表
		loadGroupMember();
		//弹出修改房管经纪人弹出框
		$("#changeAgent_dialog").dialog({
			title : '房管经纪人变更',
			autoOpen: false,
			width : 520,
			height : 240,
			modal : true
		});
	});
	//取消"变更房管经纪人"
	$("#cancel_changeAgent").click(function(){
		$("#changeAgent_dialog").dialog("close");
	});
	//确认"变更房管经纪人"
	$("#confirm_changeAgent").click(function(){
		if($("#isChangeAll").attr("checked")){
			//当前房管经纪人信息（姓名+手机号）
			var currentAgentInfo = (agentName + agentMobile);
			//新房管经纪人ID
			var newAgentId = $("select[name='newAgent']").val();
			//新房管经纪人信息
			var newAgentInfo = getNewAgentInfo(newAgentId);
			//将新/老 房管经纪人信息显示到页面
			$("#currentAgentInfo").html(currentAgentInfo);
			$("#newAgentInfo").html(newAgentInfo);
			var param = {};
			//查询当前房管经纪人所有负责房源数量
			jQuery.ajax({
				type : 'get',
				data:param,
				url : basePath + '/apply/getAgentApplyNum',
				success : function(data) {
					if(data){
						if(data.errorCode == 0){
							//填充当前房管经纪人所有负责房源数量
							$("#applyNum").html(data.applyNum);
						}else{
							$.messager.alert("操作提示", data.message, "error");
						}
					}
				}
			});
			//关闭房管经纪人变更对话框
			$("#changeAgent_dialog").dialog("close");
			$("#doubleCheck_changeAllAgent_dialog").show();
			//弹出doubleCheck提醒对话框
			$("#doubleCheck_changeAllAgent_dialog").dialog({
				title : '提醒',
				autoOpen: false,
				width : 600,
				height : 200,
				modal : true
			});
		}else{
			//执行发送变更房管经纪人请求
			changeAgent();
		}
	});
	//点击doubleCheck弹出框的"取消"按钮
	$("#cancel_changeAllAgent").click(function(){
		//关闭提醒对话框和房管经纪人变更对话框
		$("#changeAgent_dialog").dialog("close");
		$("#doubleCheck_changeAllAgent_dialog").dialog("close");
	});
	//点击doubleCheck弹出框的"确认"按钮
	$("#confirm_changeAllAgent").click(function(){
		$("#doubleCheck_changeAllAgent_dialog").dialog("close");
		changeAgent();
	});

}
//根据新房管经纪人id获取经纪人信息
function getNewAgentInfo(agentId){
	var agent;
	if(window.agentIdAgentMap){//缓存存在
		//agentId为key, agent为value
		agent = window.agentIdAgentMap[agentId];
		if(agent){
			return (agent.agentName + agent.mobile);
		} 
	}
	if(window.candidateAgents){
		window.agentIdAgentMap = {};
		var length = window.candidateAgents.length;
		for(var i=0; i<length; i++){
			var agent = window.candidateAgents[i];
			//agentId为key, agent为value
			window.agentIdAgentMap[agent.agentId] = agent;
		}
		agent = window.agentIdAgentMap[agentId];
		if(agent){
			return (agent.agentName + agent.mobile);
		} 
	}
	return "";
}
//执行发送变更房管经纪人请求
function changeAgent(){
	var param = {};
	param.toId = $("select[name='newAgent']").val();
	param.applyId = applyId;//applyId为全局变量
	param.ifAll = ($("#isChangeAll").attr("checked") ? 1 : 0);//1:变更全部,0:变更单个
	//关闭房管经纪人变更对话框
	$("#changeAgent_dialog").dialog("close");
	jQuery.ajax({
		type : 'get',
		data:param,
		url : basePath + '/apply/changeAgent',
		success : function(data) {
			if(data){
				if(data.errorCode == 0){
					$.messager.alert("操作提示", "房管经纪人变更成功！","info");
				}else{
					$.messager.alert("操作提示", "房管经纪人变更失败！","error");
				}
			}
		}
	});
}
//加载组内经纪人列表
function loadGroupMember(){
	if(window.candidateAgents){
		//使用缓存数据
		fillNewAgentList(window.candidateAgents);
		return;
	}
	var param = {};
	jQuery.ajax({
		type : 'get',
		data:param,
		url : basePath + '/apply/getGroupMember',
		success : function(data) {
			fillNewAgentList(data);
		}
	});
}
//填充变更房管经纪人的新房管经纪人列表
function fillNewAgentList(data){
	if(data){
		window.candidateAgents = data;//将结果缓存起来，下次就不需要重复查询经纪人列表
		var length = data.length;
		var options = "";
		for(var i=0; i<length; i++){
			options += "<option value='" + data[i].agentId + "'>" + data[i].agentName + " "+ data[i].mobile + "</option>";
		}
		$("select[name='newAgent']").html(options);
	}
}
//绑定文本框的默认值(鼠标移入移除时隐藏和显示)
function bindPlaceHolder(elementId, defaultText){
	$("#" + elementId).focus(function(){
		if(this.value == defaultText){
			this.value = '';
		}
	});
	$("#" + elementId).blur(function(){
		if($.trim(this.value) == ''){
			this.value = defaultText;
		}
	});
	$("#" + elementId).val(defaultText);
}
//绑定审核报备房源事件处理
function bindCheckApplyHandler(){
	//1报备成功 3报备失败
	$("#checkFail").click(function(){
		checkApply(3);
	});
	$("#checkSuccess").click(function(){
		checkApply(1);
	});
}
//审核报备房源
function checkApply(status){
	//1报备成功 3报备失败
	var param = {};
	if(status == 3){
		//审核失败，需要输入失败原因(50个汉字以内)
		var checkFailReason = $.trim($("#checkFailReason").val());
		if(!checkFailReason || checkFailReason.length == 0 || checkFailReason == "输入50字以内的审核失败原因"){
			$.messager.alert("操作提示", "请输入失败原因","info");
			return false;
		}
		if(checkFailReason.length > 50){
			//当输入的内容长度超过50字时，后面输入无效
			checkFailReason = checkFailReason.substr(0, 50);
		}
		//审核失败原因
		param.failedMemo = checkFailReason;
	}
	//审核状态
	param.status = status;
	//审核报备ID
	param.applyId = applyId;
	jQuery.ajax({
		type : 'get',
		data:param,
		url : basePath + '/apply/checkApply',
		success : function(data) {
			if(data){
				if(data.errorCode == 0){
					if(status == 1){
						jQuery.messager.confirm('提醒','审核成功，新增委托合同？',function(e){
				    		if(e){ 
				    			$.messager.alert("操作提示", "操作成功！","info");
				    		}
				    	}); 
					}else if(status == 3){
						$.messager.alert("操作提示", "操作成功！","info");
					}
					//隐藏审核div
					$("#checkApply_div").hide();
				}else{
					$.messager.alert("操作提示", "操作失败: " + data.message, "error");
				}
			}
		}
	});
};
$(document).ready(function () {
    var applyLog = new ApplyLog();
    applyLog.action = "apply";
    applyLog.method = "getApplyLog";
    applyLog.init();

    //文本输入框默认值设置
    bindPlaceHolder("checkFailReason", "输入50字以内的审核失败原因");
    //绑定修改房管经纪人事件处理
    bindChangeAgentHandler();
    //绑定审核报备房源事件处理
    bindCheckApplyHandler();
});